package com.feel.service.impl;

import com.feel.mapper.PartMapper;
import com.feel.pojo.Part;
import com.feel.service.PartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-03-20:19
 * @Description:
 */
@Service
public class PartServiceImpl implements PartService {
    @Autowired
    private PartMapper partMapper;
    @Override
    public PageInfo<Part> getAll(Integer page, Part part) {
        PageHelper.startPage(page, 8);
        List<Part> list = partMapper.getAll(part);
        PageInfo<Part> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Boolean updateInfo(Part part) {
        Integer i = partMapper.updateInfo(part);
        return i > 0;
    }

    @Override
    public Boolean addInfo(Part part) {
        Part byNameSupplier = partMapper.getByNameSupplier(part.getPartName(), part.getSupplier());
        if (byNameSupplier != null) {
            // todo 如果添加的配件信息数据库中存在，那么改成更新库存信息，但是这里没有进行更新售价和进价，有机会再更改
            Integer i = partMapper.useAddPart(part.getQuantity(), byNameSupplier.getPartId());
            return i > 0;
        }
        Integer i = partMapper.addInfo(part);
        return i > 0;
    }

    @Override
    public Part getById(Integer id) {
        Part byId = partMapper.getById(id);
        return byId;
    }

    @Override
    public List<Part> getList() {
        List<Part> list = partMapper.getAll(null);
        return list;
    }
}
